package main

import (
	"github.com/astaxie/beego"
	"github.com/astaxie/beego/orm"
	_ "github.com/go-sql-driver/mysql" // import your used driver
	"fmt"
)

// Model Struct
type Test struct {
	Id   int
	Name string `orm:"size(100)"`
}

func init() {

	beego.LoadAppConfig("ini","conf1/app.conf")

	mysqluser := beego.AppConfig.String("mysqluser")
	mysqlpass := beego.AppConfig.String("mysqlpass")
	mysqlip := beego.AppConfig.String("mysqlip")
	mysqldb := beego.AppConfig.String("mysqldb")
	dataSource := mysqluser + ":" + mysqlpass + "@tcp(" + mysqlip + ":3306)/" + mysqldb + "?charset=utf8"
	fmt.Println(dataSource)
	// set default database
	orm.RegisterDataBase("default", "mysql", dataSource, 30)

	// register model
	orm.RegisterModel(new(Test))

	// create table
	orm.RunSyncdb("default", false, true)
}

func main() {
	o := orm.NewOrm()
	var maps []orm.Params
	num, err := o.Raw("SELECT id,name FROM test").Values(&maps)
	if err == nil && num > 0 {
		fmt.Println(maps[1]["id"]) // slene
	}

	var lists []orm.ParamsList
	num, err = o.Raw("SELECT id,name FROM test").ValuesList(&lists)
	if err == nil && num > 0 {
		for _, value := range lists {
			fmt.Println(value[0], value[1])
		}
		fmt.Println(lists) // slene
	}

	var lists2 orm.ParamsList
	num, err = o.Raw("SELECT id,name FROM test").ValuesFlat(&lists2)
	if err == nil && num > 0 {
		fmt.Println(lists2) // slene
	}
}
